﻿// FilterTest.cpp : Defines the entry point for the application.
//

#include "FilterTest.h"

using namespace std;
using namespace cv;

void sharp(const Mat& src, Mat& dst, const int& Radius, const int& Threshold, const int& Amount) {
	int height = src.rows;
	int width = src.cols;
	GaussianBlur(src, dst, cv::Size(Radius, Radius), 2, 2);

	for (int h = 0; h < height; ++h) {
		for (int w = 0; w < width; ++w) {
			int bValue = src.at<Vec3b>(h, w)[0] - dst.at<Vec3b>(h, w)[0];
			int gValue = src.at<Vec3b>(h, w)[1] - dst.at<Vec3b>(h, w)[1];
			int rValue = src.at<Vec3b>(h, w)[2] - dst.at<Vec3b>(h, w)[2];
			if (abs(bValue) > Threshold) {
				bValue = src.at<Vec3b>(h, w)[0] + Amount * bValue / 100;
				if (bValue > 255)
					bValue = 255;
				else if (bValue < 0)
					bValue = 0;
				dst.at<Vec3b>(h, w)[0] = bValue;
			}
			if (abs(gValue) > Threshold) {
				gValue = src.at<Vec3b>(h, w)[1] + Amount * gValue / 100;
				if (gValue > 255)
					gValue = 255;
				else if (gValue < 0)
					gValue = 0;
				dst.at<Vec3b>(h, w)[1] = gValue;
			}
			if (abs(rValue) > Threshold) {
				rValue = src.at<Vec3b>(h, w)[2] + Amount * rValue / 100;
				if (rValue > 255)
					rValue = 255;
				else if (rValue < 0)
					rValue = 0;
				dst.at<Vec3b>(h, w)[2] = rValue;
			}
		}
	}
}
void unsharpMask(const Mat& src, Mat& dst, int radius, int threshold, int amount) {
	int height = src.rows;
	int width = src.cols;
	GaussianBlur(src, dst, cv::Size(radius, radius), 2, 2);

	for (int h = 0; h < height; ++h) {
		for (int w = 0; w < width; ++w) {
			int b = src.at<Vec3b>(h, w)[0] - dst.at<Vec3b>(h, w)[0];
			int g = src.at<Vec3b>(h, w)[1] - dst.at<Vec3b>(h, w)[1];
			int r = src.at<Vec3b>(h, w)[2] - dst.at<Vec3b>(h, w)[2];
			if (abs(b) > threshold) {
				b = src.at<Vec3b>(h, w)[0] + amount * b / 100;
				dst.at<Vec3b>(h, w)[0] = saturate_cast<uchar>(b);
			}
			if (abs(g) > threshold) {
				g = src.at<Vec3b>(h, w)[1] + amount * g / 100;
				dst.at<Vec3b>(h, w)[1] = saturate_cast<uchar>(g);
			}
			if (abs(r) > threshold) {
				r = src.at<Vec3b>(h, w)[2] + amount * r / 100;
				dst.at<Vec3b>(h, w)[2] = saturate_cast<uchar>(r);
			}
		}
	}
}

int main()
{
	Mat src = imread("C:/Users/yuhao/Desktop/haha.jpg");
	Mat dst = Mat(src.rows, src.cols, src.type());
	//sharp(src, dst, 201, 0, 50);
	unsharpMask(src, dst, 181, 0, 90);
	imshow("src", src);
	imshow("usm", dst);
	waitKey(0);
	return 0;
}





